www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/models/Advertisement.php

    <?php

class Advertisement extends CActiveRecord
{
    const YES = 1;
    const NO = 0;
    
	/**
	 * The followings are the available columns in table 'Advertisement':
	 * @var integer $id
	 * @var string $name
	 * @var string $intro
	 * @var integer $type
	 * @var integer $isvalid
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Advertisement';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('token','length','max'=>50),
		    array('name','length','max'=>50),
			array('intro','length','max'=>100),
			array('type, isvalid, locked', 'numerical', 'size_x', 'size_y', 'integerOnly'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		    'details' => array(self::HAS_MANY, 'AdDetail', 'ad_id'),
		    'current' => array(self::HAS_ONE, 'AdDetail', 'ad_id',
		        'condition' => '??.isused = 1',
		        'order' => $this->tableName() . '.id desc',
		    ),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id'=>'ID',
		    'token' => '标识',
			'name'=>'名称',
			'intro'=>'说明',
			'type'=>'类型',
		    'size_x' => '宽度',
		    'size_y' => '高度',
			'isvalid'=>'激活',
		    'locked' => '锁定',
		    'operate' => '操作'
		);
	}
	
	
	public function getSize()
	{
	    if ($this->size_x && $this->size_y)
	        $size = $this->size_x . 'x' . $this->size_y;
	    else
	        $size = null;

	    return $size;
	}
	
	
	public function getAllAvlidTokenIsKey($tokens = null)
	{
	    $cacheId = param('cacheIdExpireAdsList');
        param('caching') && $data = app()->cache->get($cacheId);
        if (!param('caching') || $data == false) {
    	    $ads = (empty($tokens)) ?
    	        $this->with('current')->findAll('isvalid = ' . self::YES) :
        	    $this->with('current')->findAllByAttributes(
        	        array('token' => $tokens),
        	        'isvalid = ' . self::YES
        	    );
    	    
    	    foreach ($ads as $ad) {
    	        $data[$ad->token] = $ad->current;
    	    }
	        unset($ads);
	        param('caching') && app()->cache->set($cacheId, $data, param('expireAdsList'));
        }
	    return $data;
	}
	
	
	public function afterDelete()
	{
	    AdDetail::model()->deleteAllByAttributes(array('ad_id' => $this->id));
	}
	
}